﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLPKWeb.Models.LinQ.DanhMucChung;
using Webdiyer.WebControls.Mvc;
using DONGACS.Common;
using QLPKWeb.Models.FormBean.DanhMucChungModel;

namespace QLPKWeb.BussinessDelegate.DanhMucChung
{
    public class NgoaiTeDelegate
    {
        DBDanhMucChungDataContext DBDanhMucChung = new DBDanhMucChungDataContext();

        public IList<dm_LoaiNgoaiTe> GetAlldm_LoaiNgoaiTe()
        {
            return DBDanhMucChung.dm_LoaiNgoaiTes.ToList();
        }


        public dm_LoaiNgoaiTe AddNewNgoaiTe(dm_LoaiNgoaiTe dm_LoaiNgoaiTe)
        {
            DBDanhMucChung.dm_LoaiNgoaiTes.InsertOnSubmit(dm_LoaiNgoaiTe);

            DBDanhMucChung.SubmitChanges();

            return GetNgoaiTeIdMax();
        }

        public void DeleteNgoaiTe(string dm_LoaiNgoaiTeID)
        {
            dm_LoaiNgoaiTe dm_LoaiNgoaiTeNew =
                DBDanhMucChung.dm_LoaiNgoaiTes.Single(p => p.Id == Convert.ToInt32(dm_LoaiNgoaiTeID));
            //dm_LoaiNgoaiTe.Id = dm_LoaiNgoaiTe.Id;

            DBDanhMucChung.dm_LoaiNgoaiTes.DeleteOnSubmit(dm_LoaiNgoaiTeNew);
            DBDanhMucChung.SubmitChanges();
        }

        public void UpdateNgoaiTe(dm_LoaiNgoaiTe dm_LoaiNgoaiTe)
        {
            dm_LoaiNgoaiTe dm_LoaiNgoaiTeUpdate =
                DBDanhMucChung.dm_LoaiNgoaiTes.Single(p => p.Id == dm_LoaiNgoaiTe.Id);

            dm_LoaiNgoaiTeUpdate.Id = dm_LoaiNgoaiTe.Id;
            dm_LoaiNgoaiTeUpdate.Ten = dm_LoaiNgoaiTe.Ten;
            dm_LoaiNgoaiTeUpdate.Ten = dm_LoaiNgoaiTe.Ten;

            DBDanhMucChung.SubmitChanges();
        }

        public PagedList<dm_LoaiNgoaiTe> SearchNgoaiTePagedList(int? id, string searchCriteria)// id = page Index
        {
            //IQueryable<dm_LoaiNgoaiTe> list = DBDanhMucChung.dm_LoaiNgoaiTes.Where(p => p.Ten == searchCriteria);
            //DBDanhMucChung.dm_LoaiNgoaiTes.ToPagedList(list,)

            return (PagedList<dm_LoaiNgoaiTe>)DBDanhMucChung.dm_LoaiNgoaiTes.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage).Where(p => p.Ten == searchCriteria);

        }

        public dm_LoaiNgoaiTe GetNgoaiTeById(string id)
        {
            try
            {
                dm_LoaiNgoaiTe item = this.DBDanhMucChung.dm_LoaiNgoaiTes.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public dm_LoaiNgoaiTe GetNgoaiTeByTen(string Ten)
        {
            //dm_LoaiNgoaiTe item = this.DBDanhMucChung.dm_LoaiNgoaiTes.Where(p => p.Ten == ten).Count() != 0 ? this.DBDanhMucChung.dm_LoaiNgoaiTes.Where(p => p.Ten == ten).ElementAt(0) : null ;

            IEnumerable<dm_LoaiNgoaiTe> list = this.DBDanhMucChung.dm_LoaiNgoaiTes.Where(p => p.Ten == Ten);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }

        public PagedList<dm_LoaiNgoaiTe> GetNgoaiTePagedList(int? id)// id = page Index
        {
            return DBDanhMucChung.dm_LoaiNgoaiTes.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }

        public dm_LoaiNgoaiTe BuildNewNgoaiTe(NgoaiTeForm form)
        {
            dm_LoaiNgoaiTe newdm_LoaiNgoaiTe = new dm_LoaiNgoaiTe();
            newdm_LoaiNgoaiTe.Ten = form.TenNgoaiTe;
         
            return newdm_LoaiNgoaiTe;
        }

        public dm_LoaiNgoaiTe BuildEditNgoaiTe(NgoaiTeForm form)
        {
            dm_LoaiNgoaiTe newdm_LoaiNgoaiTe = new dm_LoaiNgoaiTe();
            newdm_LoaiNgoaiTe.Id = form.Id;
            newdm_LoaiNgoaiTe.Ten = form.TenNgoaiTe;
            return newdm_LoaiNgoaiTe;
        }

        public dm_LoaiNgoaiTe GetNgoaiTeIdMax()
        {
            IList<dm_LoaiNgoaiTe> list = DBDanhMucChung.dm_LoaiNgoaiTes.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }

        public SelectList BuilDVT(int? selectedValue)
        {
            IList<dm_LoaiNgoaiTe> selectNCC = DBDanhMucChung.dm_LoaiNgoaiTes.OrderBy(p => p.Id).ToList();
            IList<object> selectList = new List<object>();
            int dem = 0;
            foreach (var o in selectNCC)
            {
                dem++;
                if (selectedValue != 0 || dem == selectedValue)
                {
                    selectList.Add(new { value = o.Id, Text = o.Ten, SelectedValue = selectedValue });
                }
                else
                {
                    selectList.Add(new { value = o.Id, Text = o.Ten });
                }
            }
            SelectList select = new SelectList(selectList, "Value", "Text", selectedValue);
            return select;
        }
    }
}